package com.imay.live.activity.authentication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.base.util.StringUtil;
import com.imay.live.activity.profit.IncomeActivity;
import com.imay.live.activity.user.ResigterActivity;
import com.imay.live.helper.DialogFragmentHelper;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ToastUtil;
import com.imay.live.view.custom.EditTextView;
import com.imay.live.view.custom.ReallyNameView;
import com.imay.live.R;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * create by lzx
 * 我的->设置->大V认证->信息认证绑定
 */
public class InfoBindActivity extends BaseActivity implements View.OnClickListener {
    LinearLayout src_linear;
    ReallyNameView phoneView;
    ReallyNameView reallyNameView;
    public static final int BIND = 0x1102;
    public static final String REALLYNAME = "reallyname";
    public static final String PHONE = "phone";
    public static final String UID = "uid";
    boolean name;
    long phone;
    long uid;
    TextView tv_button;
    EditTextView desText;

    public static void startActivityResult(Activity activity, boolean name, long phone, long uid) {
        Intent starter = new Intent(activity, InfoBindActivity.class);
        starter.putExtra(REALLYNAME, name);
        starter.putExtra(PHONE, phone);
        starter.putExtra(UID, uid);
        activity.startActivityForResult(starter, InfoBindActivity.BIND);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_infobind_layout;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(REALLYNAME, name);
        outState.putLong(PHONE, phone);
        outState.putLong(UID, uid);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.vsign_title));
        src_linear = $(R.id.src_linear);
        if (savedInstanceState != null) {
            name = savedInstanceState.getBoolean(REALLYNAME, false);
            phone = savedInstanceState.getLong(PHONE, 0);
            uid = savedInstanceState.getLong(UID, 0);
        } else {
            Intent intent = getIntent();
            name = intent.getBooleanExtra(REALLYNAME, false);
            phone = intent.getLongExtra(PHONE, 0);
            uid = intent.getLongExtra(UID, 0);
        }
        reallyNameView = new ReallyNameView(InfoBindActivity.this, 0);
        initName();
        phoneView = new ReallyNameView(InfoBindActivity.this, 1);
        if (phone > 0) {
            phoneView.setText(String.valueOf(phone));
            phoneView.setRightVisibility(View.GONE);
        }
        desText = new EditTextView(InfoBindActivity.this, 2);
        src_linear.addView(reallyNameView, 0);
        src_linear.addView(phoneView, 1);
        src_linear.addView(desText, 2);
        tv_button = $(R.id.tv_button);

        setButtonStatus();
        desText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setButtonStatus();
            }
        });
    }

    private void setButtonStatus() {
        if (name && phone > 0 && !StringUtil.isEmpty(desText.getTextString())) {
            tv_button.setBackground(getResources().getDrawable(R.drawable.shape_corner100_9e));
            tv_button.setOnClickListener(this);
        } else {
            tv_button.setBackground(getResources().getDrawable(R.drawable.shape_corner100_b2));
            tv_button.setOnClickListener(null);
        }
        tv_button.invalidate();
    }

    private void initName() {
        if (name) {
            reallyNameView.setText(getResources().getString(R.string.vsign_applysuccess));
            reallyNameView.setRightVisibility(View.GONE);
        }
    }

    private void putinfo(String verifiedContent) {
        if (!StringUtil.isEmpty(verifiedContent)) {
            DialogFragmentHelper.showLoadingDialog(getSupportFragmentManager(), getResources().getString(R.string.loading));
            ProtoRequestHelper.task(ProtoHttpFactory.getlargeVipSubmit(verifiedContent)).setTag(this.hashCode()).request(new ProtoCallBack() {
                @Override
                public void onRequestSuccess(String command, String data) {
                    DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
                    Result<String> result = JSON.parseObject(data, new TypeReference<Result<String>>() {
                    }.getType());
                    if (result.result == CodeSuccess) {
                        DialogFragmentHelper.showConfirmDialog(getSupportFragmentManager(), getResources().getString(R.string.exchangsuccess), getResources().getString(R.string.vsign_success), getResources().getString(R.string.buy_OK), getResources().getString(R.string.buy_cancel), true, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                setResult(RESULT_OK);
                                finish();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
                    } else {
                        ToastUtil.show(InfoBindActivity.this, getResources().getString(R.string.vsign_fail));
                    }
                }

                @Override
                public void onRequestError(String command, int code, String message) {
                    DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
                    ToastUtil.show(InfoBindActivity.this, getResources().getString(R.string.vsign_fail));
                }
            });
        } else {
            ToastUtil.show(InfoBindActivity.this, getResources().getString(R.string.vsign_deshint));
        }

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_button) {
            putinfo(desText.getTextString());
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IncomeActivity.RECODE) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    name = data.getBooleanExtra(REALLYNAME, false);
                    if (name) {
                        initName();
                        setButtonStatus();
                    }
                }
            }
        } else if (requestCode == IncomeActivity.RECARDCODE) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    phone = data.getLongExtra(PHONE, 0);
                    if (phone > 0) {
                        phoneView.setText(String.valueOf(phone));
                        phoneView.setRightVisibility(View.GONE);
                        setButtonStatus();
                    }
                }
            }
        }
    }
}
